import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';
export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async toOrderDetali(e, recordId) {
      console.log(
        {
          goodsInfoIds: e.goodsInfoDTOS.map((e) => e.goodsInfoId),
          giftOrder: true,
          recordId,
        },
        e,
      );
      const toBay = await api.MyGiftPackController.freeBuy({
        goodsInfoIds: e.goodsInfoDTOS.map((e) => e.goodsInfoId),
        giftOrder: true,
        recordId,
      });
      Taro.navigateTo({
        url: '/pages/package-C/order/order-confirm-gift/index',
      });
    },

    async page() {
      try {
        const {
          main: {searchForm, list},
        } = getData();
        const searchParams = JSON.parse(JSON.stringify(searchForm));
        searchParams.pageNum++;
        const result = await api.MyGiftPackController.getMyGiftPackList(searchParams);
        action.commonChange('main.list', [...list, ...result.content]);
        let type = 'more';
        if (result.content.length <= 0 || result.last) {
          type = 'end';
        }
        if (result.content.length <= 0 && list.length <= 0) {
          type = 'last';
        }
        action.commonChange('main.type', type);
        action.commonChange('main.searchForm.pageNum', searchParams.pageNum);
      } catch (error) {
        console.log(error, '我是错误');
      }
    },
    /**
     * 初始化
     */
    async query() {},
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageBMyGiftPackMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
